package com.infotel.prefecture.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;

import com.infotel.prefecture.exception.PrefectureException;
import com.infotel.prefecture.model.Vehicule;

public class VehiculeDAOJdbc extends AbstractDAOJdbc implements VehiculeDAO {

    @Override
    public void insertVehicule(Vehicule vehicule) throws PrefectureException {
        
        Connection cx = null;
        PreparedStatement ps = null;
        try {
            cx = getConnection();
            String sql = "insert into vehicule (veh_num_serie, veh_mod_id, veh_places_assis, veh_poids_vide, veh_date_mise_circ, veh_co2, veh_puiss) values (?,?,?,?,?,?,?)";
            ps = cx.prepareStatement(sql);
            ps.setString(1, vehicule.getNumeroSerie());
            ps.setInt(2, vehicule.getModele().getId());
            ps.setInt(3, vehicule.getNbPlace());
            ps.setInt(4, vehicule.getPoids());
            ps.setDate(5, new java.sql.Date(vehicule.getDateMiseCirculation().getTime()));
            ps.setInt(6, vehicule.getCo2());
            ps.setInt(7, vehicule.getPuissance());
           ps.executeUpdate();
        } catch (Exception e) {
            throw new PrefectureException("Echec insertion vehicule", e);
        } finally {
            close(cx, ps);
        }
    }

}
